package com.rqbl.controller;
import com.rqbl.entity.Comment;
import com.rqbl.entity.User;
import com.rqbl.myutils.PageBean;
import com.rqbl.service.CommentService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bruce
 * @since 2024-07-19
 */
@Controller
@RequestMapping("/comment")
@CrossOrigin
public class CommentController {
    @Autowired
    private CommentService commentService;

    @RequestMapping("getCommentByPage")
    @ResponseBody
    public List<Comment> getCommentByPage(Integer page, Integer pid) {
        PageBean pageBean = new PageBean();
        if(page == null)
            page = 1;
        System.out.println("page:"+page);
        System.out.println("pid:"+pid);
        pageBean.setPage(page);
        pageBean.setPageSize(2);
        return commentService.getCommentByPage(pageBean,pid);
    }

    @RequestMapping("addComment")
    @ResponseBody
    public String addComment(String text, String url,Integer pid, HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        return commentService.addComment(text,url,user.getUid(),pid).toString();
    }

}
